<?php

/*
 * Copyright 2015 gg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Com\Wechat\ChatService;

/**
 * 对话服务-发送消息
 */
class SendModel {

    /**
     * 返回文本消息
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $content 消息内容(请传入变量)
     * @return string xml字符串
     */
    public function sendText($toUser, $fromUser, &$content) {
        $timeStamp = time();
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>$timeStamp</CreateTime>"
                . "<MsgType><![CDATA[text]]></MsgType>"
                . "<Content><![CDATA[$content]]></Content></xml>";
        return $xml;
    }

    /**
     * 返回图片消息
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $media_id 上传图片获取到的mediaID
     * @return string xml字符串
     */
    public function sendImage($toUser, $fromUser, $media_id) {
        $timeStamp = time();
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>$timeStamp</CreateTime>"
                . "<MsgType><![CDATA[image]]></MsgType>"
                . "<Image><MediaId><![CDATA[$media_id]]></MediaId></Image></xml>";
        return $xml;
    }

    /**
     * 返回语音消息
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $media_id 上传语音获取到的mediaID
     * @return string xml字符串
     */
    public function sendVoice($toUser, $fromUser, $media_id) {
        $timeStamp = time();
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>$timeStamp</CreateTime>"
                . "<MsgType><![CDATA[voice]]></MsgType>"
                . "<Voice><MediaId><![CDATA[$media_id]]></MediaId></Voice></xml>";
        return $xml;
    }

    /**
     * 返回视频消息
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $media_id 上传视频获取到的mediaID
     * @param type $title 视频标题
     * @param type $description 视频简介
     * @return string xml字符串
     */
    public function sendVideo($toUser, $fromUser, $media_id, $title = "", $description = "") {
        $timeStamp = time();
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>$timeStamp</CreateTime>"
                . "<MsgType><![CDATA[video]]></MsgType>"
                . "<Video><MediaId><![CDATA[$media_id]]></MediaId>"
                . "<Title><![CDATA[$title]]></Title>"
                . "<Description><![CDATA[$description]]></Description></Video> </xml>";
        return $xml;
    }

    /**
     * 返回音乐消息
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $thumbMediaId 上传缩略图获取到的mediaId
     * @param type $title 音乐标题
     * @param type $description 音乐简介
     * @param type $musicUrl 音乐链接
     * @param type $hqMusicUrl 高清音乐链接
     * @return string xml字符串
     */
    public function sendMusic($toUser, $fromUser, $thumbMediaId, $title = "", $description = "", $musicUrl = "", $hqMusicUrl = "") {
        $timeStamp = time();
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>$timeStamp</CreateTime>"
                . "<MsgType><![CDATA[music]]></MsgType>"
                . "<Music><Title><![CDATA[$title]]></Title>"
                . "<Description><![CDATA[$description]]></Description>"
                . "<MusicUrl><![CDATA[$musicUrl]]></MusicUrl>"
                . "<HQMusicUrl><![CDATA[$hqMusicUrl]]></HQMusicUrl>"
                . "<ThumbMediaId><![CDATA[$thumbMediaId]]></ThumbMediaId></Music></xml>";
        return $xml;
    }

    /**
     * 返回图文消息
     * data数组格式：
     * 二维数组，第一维为编号，0-n，n<=9
     * 第二维为内容，下标如下：
     * title：标题 description：简介 picurl：图片链接，支持jpg/png url：跳转链接
     * 第二维若无内容请定义为null
     * @param type $toUser 接收方openID
     * @param type $fromUser 公众号openID
     * @param type $num 图文消息个数，限制为10条以内
     * @param type $data 图文消息内容，限制为10条以内
     * @return string  xml字符串
     */
    public function sendImgText($toUser, $fromUser, $num, $data) {
        $timeStamp = time();
        $items = "";
        for ($a = 0; $a < $num; $a++) {
            $item = "<item>"
                    . ((isset($data[$a]["title"])) ? ("<Title><![CDATA[" . $data[$a]["title"] . "]]></Title>") : "")
                    . ((isset($data[$a]["description"])) ? "<Description><![CDATA[" . $data[$a]["description"] . "]]></Description>" : "")
                    . ((isset($data[$a]["description"])) ? "<PicUrl><![CDATA[" . $data[$a]["picurl"] . "]]></PicUrl>" : "")
                    . ((isset($data[$a]["description"])) ? "<Url><![CDATA[" . $data[$a]["url"] . "]]></Url>" : "")
                    . "</item>";
            $items.=$item;
        }
        $xml = "<xml><ToUserName><![CDATA[$toUser]]></ToUserName>"
                . "<FromUserName><![CDATA[$fromUser]]></FromUserName>"
                . "<CreateTime>12345678</CreateTime>"
                . "<MsgType><![CDATA[news]]></MsgType>"
                . "<ArticleCount>$num</ArticleCount><Articles>$items</Articles></xml> ";
        return $xml;
    }

}
